

package com.googlecode.flac4j.format.metadata;


import java.util.SortedMap;
import java.util.TreeMap;

import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlAdapter;


/**
 * XmlAdapter for a map of numbers and indices.
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlTransient
public class TracksAdapter
    extends XmlAdapter<Tracks, SortedMap<Integer, Track>> {


    @Override
    public Tracks marshal(final SortedMap<Integer, Track> bound)
        throws Exception {

        final Tracks value = new Tracks();
        value.getTracks().addAll(bound.values());
        return value;
    }


    @Override
    public SortedMap<Integer, Track> unmarshal(final Tracks value)
        throws Exception {

        final SortedMap<Integer, Track> bound = new TreeMap<Integer, Track>();
        for (Track track : value.getTracks()) {
            bound.put(track.getNumber(), track);
        }
        return bound;
    }


}

